package com.itheima.demo07newdatetimeapi;

import org.junit.Test;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.Temporal;
import java.time.temporal.TemporalAccessor;
import java.time.temporal.TemporalAdjuster;

public class Demo02 {

    @Test
    public void test09(){
        LocalDateTime now = LocalDateTime.now();

        //将日期调整到"下一个月的第一天"操作
        TemporalAdjuster firstDayofNextMonth=temporal -> {
            LocalDateTime dateTime = (LocalDateTime) temporal;
            return dateTime.plusMonths(1).withDayOfMonth(1);
        };
        LocalDateTime newDateTime = now.with(firstDayofNextMonth);
        System.out.println("newDateTime = " + newDateTime);
    }

    //Duration/Period类:计算时间差
    @Test
    public void test08(){
        LocalTime now = LocalTime.now();
        LocalTime time = LocalTime.of(14, 15, 20);
        Duration duration = Duration.between(time, now);
        System.out.println("相差的天数"+duration.toDays());
        System.out.println("相差的小时数:"+duration.toHours());
        System.out.println("相差的分钟数:"+duration.toMinutes());
        System.out.println("相差的秒数:"+duration.toSeconds());
        //Period计算日期的距离
        LocalDate nowDate = LocalDate.now();
        LocalDate date = LocalDate.of(1998, 8, 8);
        Period period = Period.between(date, nowDate);
        System.out.println("相差年:"+period.getYears());
        System.out.println("相差的月:"+period.getMonths());
        System.out.println("相差的天:"+period.getDays());
    }

    @Test
    public void test07(){
        Instant now = Instant.now();
        System.out.println("now = " + now);

        Instant plus = now.plusSeconds(20);
        System.out.println("plus = " + plus);

        Instant minus = now.minusSeconds(20);
        System.out.println("minus = " + minus);

        //得到纳秒
        System.out.println(now.getEpochSecond());
        System.out.println(now.getNano());
    }


    @Test
    public void test04() {
        //创建一个日期时间
        LocalDateTime now = LocalDateTime.now();

        //格式化
        //指定时间的格式
        //JDk自带的时间格式
//        DateTimeFormatter dtf = DateTimeFormatter.ISO_DATE_TIME;
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH时mm分ss秒");
//        String format = now.format(dtf);
//        System.out.println("format = " + format);

//        LocalDateTime parse = LocalDateTime.parse("2016年12月13日 12时12分16秒", dtf);
//        System.out.println("parse = " + parse);

        //解析
        for (int i = 0; i < 50; i++) {
            new Thread(() -> {
                LocalDateTime parse = LocalDateTime.parse("2016年12月13日 12时12分16秒", dtf);
                System.out.println(parse);
            }).start();
        }

    }

    @Test
    public void testEquals() {
        LocalDateTime dateTime = LocalDateTime.of(2018, 7, 12, 13, 28, 56);
        LocalDateTime now = LocalDateTime.now();

        System.out.println(now.isAfter(dateTime));
        System.out.println(now.isBefore(dateTime));
        System.out.println(now.isEqual(dateTime));

    }

    @Test
    public void testLocalDateTime2() {
        LocalDateTime now = LocalDateTime.now();
        //修改时间,修改后返回新的时间对象
        LocalDateTime dateTime = now.withYear(9102);
        System.out.println("dateTime = " + dateTime);

        //增加或减去时间
        //plus:增加制定时间
        //minus:减去指定时间
        System.out.println(now.plusYears(2));
        System.out.println(now.minusYears(10));
    }

    @Test
    public void testLocalDateTime() {
        LocalDateTime dateTime = LocalDateTime.of(2018, 7, 12, 13, 12, 11);
        System.out.println("dateTime = " + dateTime);

        LocalDateTime now = LocalDateTime.now();
        System.out.println("now = " + now);

        System.out.println(now.getYear());
        System.out.println(now.getMonthValue());
        System.out.println(now.getHour());
        System.out.println(now.getSecond());
    }

    @Test
    public void testLocalTime() {
        LocalTime time = LocalTime.of(13, 26, 39);
        System.out.println("time = " + time);

        LocalTime now = LocalTime.now();
        System.out.println("now = " + now);

        System.out.println(now.getHour());
        System.out.println(now.getMinute());
        System.out.println(now.getSecond());
        System.out.println(now.getNano());
    }

    @Test
    public void testLocalDate() {
        //LocalDate:表示日期，有年月日
        LocalDate now = LocalDate.now();
        System.out.println("now = " + now);

        LocalDate date = LocalDate.of(2018, 8, 8);
        System.out.println("date = " + date);

        System.out.println(now.getYear());
        System.out.println(now.getMonth().getValue());
        System.out.println(now.getDayOfMonth());
    }
}
